home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Networking / MacTCP / MPing 1.1 / Sources / MPingWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-30  |  7.7 KB  |  263 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #    MPing 1.1 - MacTCP Ping Tool
  3. #
  4. #    Copyright © Apple Computer, Inc. 1990-1991
  5. #    All rights reserved.
  6. #
  7. #    Versions:    
  8. #                1.1        September 25, 1991.
  9. #
  10. #    File:
  11. #                MPing.c
  12. #
  13. #    Components:
  14. #                AddressXlation.h
  15. #                MacTCPCommonTypes.h
  16. #                Makefile
  17. #                MiscIPPB.h
  18. #                MPing.c
  19. #                MPing.h
  20. #                MPing.r
  21. #                MPingDlg.c
  22. #                MPingExtern.h
  23. #                MPingGlobals.h
  24. #                MPingIcmp.c
  25. #                MPingWindow.c
  26. #                resolver.c
  27. ------------------------------------------------------------------------------*/
  28.  
  29. #include <Values.h>
  30. #include <Types.h>
  31. #include <Resources.h>
  32. #include <QuickDraw.h>
  33. #include <Fonts.h>
  34. #include <Events.h>
  35. #include <Windows.h>
  36. #include <Menus.h>
  37. #include <TextEdit.h>
  38. #include <Dialogs.h>
  39. #include <Desk.h>
  40. #include <ToolUtils.h>
  41. #include <Memory.h>
  42. #include <SegLoad.h>
  43. #include <Files.h>
  44. #include <OSUtils.h>
  45. #include <OSEvents.h>
  46. #include <DiskInit.h>
  47. #include <Packages.h>
  48. #include <Traps.h>
  49.  
  50. #include "MacTCPCommonTypes.h"
  51. #include "MPing.h"                /* bring in all the #defines for MPing */
  52. #include "MPingGlobals.h"        /* bring in some structure definitions */
  53. #include "MPingExtern.h"        /* all extern variables */
  54.  
  55.  
  56. void AdjustHV(Boolean isVert, ControlHandle control, TEHandle docTE, Boolean canRedraw);
  57. void AdjustScrollValues(WindowPtr window, Boolean canRedraw);
  58. pascal void VActionProc(ControlHandle control, short part);
  59. void CommonAction(ControlHandle control, short *amount);
  60. void AdjustScrollbars(WindowPtr window, Boolean needsResize);
  61. void AdjustScrollSizes(WindowPtr window);
  62. void GetTERect(WindowPtr window, Rect *teRect);
  63. void AdjustViewRect(TEHandle docTE);
  64. void AdjustTE(WindowPtr window);
  65.  
  66.  
  67. /* Simply call the common adjust routine for the vertical and horizontal scrollbars. */
  68.  
  69. #pragma segment Window
  70. void AdjustScrollValues(window, canRedraw)
  71. WindowPtr    window;
  72. Boolean        canRedraw;
  73. {
  74.     DocumentPeek doc;
  75.     
  76.     doc = (DocumentPeek) window;
  77.     AdjustHV(true, doc->docVScroll, doc->docTE, canRedraw);
  78. } /*AdjustScrollValues*/
  79.  
  80.  
  81. /* Calculate the new control maximum value and current value, whether it is the horizontal or
  82.     vertical scrollbar. The vertical max is calculated by comparing the number of lines to the
  83.     vertical size of the viewRect. The horizontal max is calculated by comparing the maximum document
  84.     width to the width of the viewRect. The current values are set by comparing the offset between
  85.     the view and destination rects. If necessary and we canRedraw, have the control be re-drawn by
  86.     calling ShowControl. */
  87.  
  88. #pragma segment Main
  89. void AdjustHV(isVert, control, docTE, canRedraw)
  90. Boolean        isVert;
  91. ControlHandle control;
  92. TEHandle    docTE;
  93. Boolean        canRedraw;
  94. {
  95.     short        value, lines, max;
  96.     short        oldValue, oldMax;
  97.     TEPtr        te;
  98.     
  99.     oldValue = GetCtlValue(control);
  100.     oldMax = GetCtlMax(control);
  101.     te = *docTE;                            /* point to TERec for convenience */
  102.     if ( isVert ) {
  103.         lines = te->nLines;
  104.         /* since nLines isn’t right if the last character is a return, check for that case */
  105.         if ( *(*te->hText + te->teLength - 1) == kCrChar )
  106.             lines += 1;
  107.         max = lines - ((te->viewRect.bottom - te->viewRect.top) /
  108.                 te->lineHeight);
  109.     } else
  110.         max = kMaxDocWidth - (te->viewRect.right - te->viewRect.left);
  111.     
  112.     if ( max < 0 ) max = 0;
  113.     SetCtlMax(control, max);
  114.     
  115.     /* Must deref. after SetCtlMax since, technically, it could draw and therefore move
  116.         memory. This is why we don’t just do it once at the beginning. */
  117.     te = *docTE;
  118.     if ( isVert )
  119.         value = (te->viewRect.top - te->destRect.top) / te->lineHeight;
  120.     else
  121.         value = te->viewRect.left - te->destRect.left;
  122.     
  123.     if ( value < 0 ) value = 0;
  124.     else if ( value >  max ) value = max;
  125.     
  126.     SetCtlValue(control, value);
  127.     /* now redraw the control if it needs to be and can be */
  128.     if ( canRedraw || (max != oldMax) || (value != oldValue) )
  129.         ShowControl(control);
  130. } /*AdjustHV*/
  131.  
  132. /* Determines how much to change the value of the vertical scrollbar by and how
  133.     much to scroll the TE record. */
  134.  
  135. #pragma segment Main
  136. pascal void VActionProc(control,part)
  137.     ControlHandle control;
  138.     short        part;
  139. {
  140.     short        amount;
  141.     WindowPtr    window;
  142.     TEPtr        te;
  143.     
  144.     if ( part != 0 ) {                /* if it was actually in the control */
  145.         window = (*control)->contrlOwner;
  146.         te = *((DocumentPeek) window)->docTE;
  147.         switch ( part ) {
  148.             case inUpButton:
  149.             case inDownButton:        /* one line */
  150.                 amount = 1;
  151.                 break;
  152.             case inPageUp:            /* one page */
  153.             case inPageDown:
  154.                 amount = (te->viewRect.bottom - te->viewRect.top) / te->lineHeight;
  155.                 break;
  156.         }
  157.         if ( (part == inDownButton) || (part == inPageDown) )
  158.             amount = -amount;        /* reverse direction for a downer */
  159.         CommonAction(control, &amount);
  160.         if ( amount != 0 )
  161.             TEScroll(0, amount * te->lineHeight, ((DocumentPeek) window)->docTE);
  162.     }
  163. } /* VActionProc */
  164.  
  165. /*    Common algorithm for pinning the value of a control. It returns the actual amount
  166.     the value of the control changed. Note the pinning is done for the sake of returning
  167.     the amount the control value changed. */
  168.  
  169. #pragma segment Main
  170. void CommonAction(control,amount)
  171.     ControlHandle control;
  172.     short        *amount;
  173. {
  174.     short        value, max;
  175.     
  176.     value = GetCtlValue(control);    /* get current value */
  177.     max = GetCtlMax(control);        /* and maximum value */
  178.     *amount = value - *amount;
  179.     if ( *amount < 0 )
  180.         *amount = 0;
  181.     else if ( *amount > max )
  182.         *amount = max;
  183.     SetCtlValue(control, *amount);
  184.     *amount = value - *amount;        /* calculate the real change */
  185. } /* CommonAction */
  186.  
  187.  
  188. /* Turn off the controls by jamming a zero into their contrlVis fields (HideControl erases them
  189.     and we don't want that). If the controls are to be resized as well, call the procedure to do that,
  190.     then call the procedure to adjust the maximum and current values. Finally re-enable the controls
  191.     by jamming a $FF in their contrlVis fields. */
  192.  
  193. #pragma segment Main
  194. void AdjustScrollbars(window, needsResize)
  195.     WindowPtr    window;
  196.     Boolean        needsResize;
  197. {
  198.     DocumentPeek doc;
  199.     
  200.     doc = (DocumentPeek) window;
  201.     /* First, turn visibility of scrollbars off so we won’t get unwanted redrawing */
  202.     (*doc->docVScroll)->contrlVis = kControlInvisible;    /* turn them off */
  203.     if ( needsResize )                                    /* move & size as needed */
  204.         AdjustScrollSizes(window);
  205.     AdjustScrollValues(window, needsResize);            /* fool with max and current value */
  206.     /* Now, restore visibility in case we never had to ShowControl during adjustment */
  207.     (*doc->docVScroll)->contrlVis = kControlVisible;    /* turn them on */
  208. } /* AdjustScrollbars */
  209.  
  210. /*    Re-calculate the position and size of the viewRect and the scrollbars.
  211.     kScrollTweek compensates for off-by-one requirements of the scrollbars
  212.     to have borders coincide with the growbox. */
  213.  
  214. #pragma segment Main
  215. void AdjustScrollSizes(window)
  216.     WindowPtr    window;
  217. {
  218.     Rect        teRect;
  219.     DocumentPeek doc;
  220.     
  221.     doc = (DocumentPeek) window;
  222.     GetTERect(window, &teRect);                            /* start with TERect */
  223.     (*doc->docTE)->viewRect = teRect;
  224.     AdjustViewRect(doc->docTE);                            /* snap to nearest line */
  225. } /*AdjustScrollSizes*/
  226.  
  227. #pragma segment Main
  228. void GetTERect(window, teRect)
  229. WindowPtr window;
  230. Rect *teRect;
  231. {
  232.     *teRect = gMsgRect;
  233.     InsetRect(teRect, kTextMargin, kTextMargin);
  234.     teRect->right = teRect->right - 15;            /* for the vertical scrollbar */
  235. }
  236.  
  237. #pragma segment Main
  238. void AdjustViewRect(docTE)
  239. TEHandle    docTE;
  240. {
  241.     TEPtr        te;
  242.     
  243.     te = *docTE;
  244.     te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
  245.                             * te->lineHeight) + te->viewRect.top;
  246. } /*AdjustViewRect*/
  247.  
  248. /* Scroll the TERec around to match up to the potentially updated scrollbar
  249.     values. This is really useful when the window has been resized such that the
  250.     scrollbars became inactive but the TERec was already scrolled. */
  251.  
  252. #pragma segment Main
  253. void AdjustTE(window)
  254.     WindowPtr    window;
  255. {
  256.     TEPtr        te;
  257.     
  258.     te = *((DocumentPeek)window)->docTE;
  259.     TEScroll(0, (te->viewRect.top - te->destRect.top) -
  260.             (GetCtlValue(((DocumentPeek)window)->docVScroll) * te->lineHeight),
  261.             ((DocumentPeek)window)->docTE);
  262. } /*AdjustTE*/
  263.